const app = getApp()
var flag = false;
Page({
  data: {
    baseurl: app.globalData.baseUrl ,

    addressId: "",
    addressUserName: "",
    addressBuyerPhone: "",
    addressArea: "",
    addressDetail: "",
    recommendedPrice: "",
    category2Name: "",
    orderRemark: "",
    workerName:"",
    workerPhone:"",

    //预约时间
    serviceTime: ''
    
  },
  onLoad: function (options) {
    
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    var openid = wx.getStorageSync("openid");
    var orderId = options.orderId
    console.log("openid", openid)
    console.log("orderId", orderId)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderuserinfo',
      method: 'GET',
      data: { "buyerOpenid": openid, "orderId": orderId},//传入的参数
      success: function (res) {
        var list = res.data.orderList[0];
        var category2Name = res.data.orderList[0].orders[0].orderDetails[0].category2.category2Name;
        var statusNumber = res.data.orderList[0].orders[0].orderStatus
        if (statusNumber==0){
          var orderStatus="待接单"
        } else if (statusNumber == 1){
          var orderStatus = "已接单"
        } else if (statusNumber == 2) {
          var orderStatus = "已完成"
        } else if (statusNumber == 3) {
          var orderStatus = "已取消"
        }
        console.log("list", list)
        console.log("category2Name", category2Name)
        
        /////////
        var worker = res.data.worker;
        console.log("worker", worker)
        var workerId=null
        var workerName = null
        var workerPhone = null
        if (worker!=null){
          workerId= worker.id
          workerName=worker.name
          workerPhone= worker.phone
        }

        //////////////
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {

          
          that.setData({
            //orderlist: list,
            orderStatus: orderStatus,
            addressUserName: list.addressUserName,
            addressBuyerPhone: list.addressBuyerPhone,
            addressArea: list.addressArea,       
            addressDetail: list.addressDetail,
            category2Name: list.orders[0].orderDetails[0].category2.category2Name,
            serviceTime: list.orders[0].serviceTime,
            recommendedPrice: list.orders[0].orderAmount,
            //imagesList: imagesList,
            orderRemark: list.orders[0].orderRemark,
            workerId: workerId,
            workerName: workerName,
            workerPhone: workerPhone,
            workerRemark: list.orders[0].workerRemark,
            orderStatus0: list.orders[0].orderStatus,
            createTime1: list.orders[0].createTime1,
            updateTime1: list.orders[0].updateTime1
            
          });

          if (list.orders[0].picture != '' && list.orders[0].picture != 'null' && list.orders[0].picture != null){
            console.log("picture:", list.orders[0].picture)
            var imagesList = list.orders[0].picture.split(",");
            console.log("imagesList:", imagesList)
            for (var i = 0; i < imagesList.length;i++){
              imagesList[i] = app.globalData.baseUrl + imagesList[i];
            }
            that.setData({
              
              imagesList: imagesList,
              
            });

          }
        }
      }
    })
  
  },
  /**
   * 预览图片方法
   */
  listenerButtonPreviewImage: function (e) {   
    let that = this;
    let imagesList = that.data.imagesList;
    let url = e.target.dataset.url;
    console.log(url);
    
    wx.previewImage({
      current: url,
      urls: imagesList,
      
      //这根本就不走
      success: function (res) {
        //console.log(res);
      },
      //也根本不走
      fail: function () {
        //console.log('fail')
      }
    })
  },
  
  onShow: function (options) {   
    

  }
})